package cn.stylefeng.guns.modular.business.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.pojo.node.TreeviewNode;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.modular.business.entity.Category;
import cn.stylefeng.guns.modular.business.model.result.CategoryResult;
import cn.stylefeng.guns.modular.business.service.CategoryService;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.treebuild.DefaultTreeBuildFactory;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController extends BaseController {

    private String PREFIX = "/business/category/";

    @Autowired
    private CategoryService categoryService;


    @RequestMapping("")
    public String index() {
        return PREFIX + "category.html";
    }


    @RequestMapping("/category_add")
    public String deptAdd() {
        return PREFIX + "category_add.html";
    }


    @RequestMapping("/category_update")
    public String deptUpdate(@RequestParam("categoryId") Long categoryId) {

        if (ToolUtil.isEmpty(categoryId)) {
            throw new RequestEmptyException();
        }
        Category category = categoryService.getById(categoryId);
        LogObjectHolder.me().set(category);
        return PREFIX + "category_edit.html";
    }


    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.categoryService.tree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }


    @RequestMapping(value = "/treeview")
    @ResponseBody
    public List<TreeviewNode> treeview() {
        List<TreeviewNode> treeviewNodes = this.categoryService.treeviewNodes();
        DefaultTreeBuildFactory<TreeviewNode> factory = new DefaultTreeBuildFactory<>();
        factory.setRootParentId("0");
        return factory.doTreeBuild(treeviewNodes);
    }


    @RequestMapping(value = "/add")
    @ResponseBody
    public ResponseData add(Category category) {
        this.categoryService.addCategory(category);
        return SUCCESS_TIP;
    }


    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "condition", required = false) String condition,
                       @RequestParam(value = "categoryId", required = false) String categoryId) {
        return LayuiPageFactory.createPageInfo(categoryService.list(condition, categoryId));
    }

    @RequestMapping(value = "/detail/{categoryId}")
    @ResponseBody
    public Object detail(@PathVariable("categoryId") Long categoryId) {
        Category category = categoryService.getById(categoryId);
        CategoryResult categoryResult = new CategoryResult();
        BeanUtil.copyProperties(category, categoryResult);
        if (category.getPid() == 0) {
            categoryResult.setPName("顶级");
        } else {
            Category category1 = categoryService.getById(category.getPid());
            categoryResult.setPName(category1.getName());
        }
        return categoryResult;
    }


    @RequestMapping(value = "/update")
    @ResponseBody
    public ResponseData update(Category category) {
        categoryService.editCategory(category);
        return SUCCESS_TIP;
    }


    @RequestMapping(value = "/delete")
    @ResponseBody
    public ResponseData delete(@RequestParam Long categoryId) {
        categoryService.deleteCategory(categoryId);
        return SUCCESS_TIP;
    }




}
